﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using JM.JCY.BusinessEntity;
using JM.JCY.Proxy;
using JM.JCY.Common;
using JM.JCY.Common.Utilities;
using Microsoft.SharePoint;

namespace JM.JCY.SharePointComponent.News.CreateNews
{
    public partial class CreateNewsUserControl : UserControl
    {
        public string _category
        {
            set { ViewState["_category"] = value; }
            get { return (string)ViewState["_category"]; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["category"] != null)
                {
                    _category = Request.QueryString["category"].ToString();
                    this.hfCategory.Value = _category;
                }

                if (_category == "News")
                {

                    this.lblDisplay.Text = "创建新闻";
                }
                else
                {   

                    this.lblDisplay.Text = "创建领导讲话";
                }
            }


        }

        protected NewsDataSet.T_AnnouncementDataTable GetNewsDataFromPage(string status)
        {
            bool isUpdate = false;
            NewsDataSet.T_AnnouncementDataTable dt = new NewsDataSet.T_AnnouncementDataTable();
            if (Session["NewsData"] != null)
            {
                dt = (NewsDataSet.T_AnnouncementDataTable)Session["NewsData"];
                isUpdate = true;
            }

            NewsDataSet.T_AnnouncementRow newsRow;
            if (isUpdate == false)
                newsRow = dt.NewT_AnnouncementRow();
            else
                newsRow = (NewsDataSet.T_AnnouncementRow)dt.Rows[0];

            newsRow.Subject = txtSubject.Text.Trim();
            newsRow.Summary = txtSummary.Text.Trim();

            if (this.filePicture.HasFile)
            {
                newsRow.ImageUrl = JCYSharePointHelper.UploadPublishedFile(this.filePicture, Guid.NewGuid().ToString(), JCYConstant.SharePointListName.NewsPictureLibrary);
                this.hfPicPath.Value = newsRow.ImageUrl;
            }

            newsRow.Status = status;
            Common.JCYUtility.UpdateCommonFields(newsRow, SPContext.Current.Web.CurrentUser.Name);
            if (isUpdate == false)
                dt.AddT_AnnouncementRow(newsRow);

            return dt;
        }

        protected void imgPreview_Click(object sender, ImageClickEventArgs e)
        {
            NewsDataSet.T_AnnouncementDataTable dt = GetNewsDataFromPage("");
            Session["NewsData"] = dt;
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Popup", "popup('/_layouts/SharePointComponent/PreviewNews.aspx');", true);
        }

        protected void imgSaveDraft_Click(object sender, ImageClickEventArgs e)
        {
            CreateNews(_category, JCYConstant.Status.Draft);
            Response.Redirect("ManageNews.aspx?Category=" + _category);
        }

        protected void imgSavePublish_Click(object sender, ImageClickEventArgs e)
        {
            CreateNews(_category, JCYConstant.Status.Publish);
            Response.Redirect("ManageNews.aspx?Category=" + _category);
        }

        private void CreateNews(string category, string status)
        {
            using (NewsProxy newsProxy = new NewsProxy())
            {
                NewsDataSet.T_AnnouncementDataTable newDT = new NewsDataSet.T_AnnouncementDataTable();
                NewsDataSet.T_AnnouncementRow newRow = newDT.NewT_AnnouncementRow();
                newRow.Subject = this.txtSubject.Text;
                newRow.Summary = this.txtSummary.Text;
                newRow.Status = status;
                newRow.Category = category;

                if (this.filePicture.HasFile)
                {
                    newRow.ImageUrl = JCYSharePointHelper.UploadPublishedFile(this.filePicture, Guid.NewGuid().ToString(), JCYConstant.SharePointListName.NewsPictureLibrary);
                }
                else if (this.hfPicPath.Value != string.Empty)
                {
                    newRow.ImageUrl = this.hfPicPath.Value;
                }
                else
                {
                    newRow.ImageUrl = string.Empty;
                }


                Common.JCYUtility.UpdateCommonFields(newRow, SPContext.Current.Web.CurrentUser.Name);
                newDT.AddT_AnnouncementRow(newRow);


                newsProxy.UpdateNews(newDT);
            }
        }

        protected void imgCancel_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("ManageNews.aspx?Category=" + _category);
        }
    }
}
